package com.atguigu.srb.core.controller.api;


import com.atguigu.common.results.R;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.service.UserAccountService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author Atguigu
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/api/core/userAccount")
public class ApiUserAccountController {
    @Autowired
    UserAccountService userAccountService;
    //http://192.168.1.170/api/core/userAccount/notifyWithdraw
    //提现回调接口
    @ApiOperation("提现回调")
    @PostMapping("/notifyWithdraw")
    public String notifyWithdraw(HttpServletRequest request){
        String result = userAccountService.notifyWithdraw(request);
        return result;
    }
    //提现接口: 返回自动提交的表单字符串
    @ApiOperation("会员申请提现")
    @GetMapping("/auth/commitWithdraw/{amount}")
    public R commitWithdraw(@RequestHeader(value = "token",required = false)
                                        String token,
                            @PathVariable("amount")BigDecimal amount){
        String formStr = userAccountService.commitWithdraw(token,amount);
        return R.success().data("formStr" , formStr);
    }

    @ApiOperation("获取投资金额收益")
    //还款方式不一样，收益算法不一样：还款方式
    @GetMapping("getInterestCount/{returnMethod}/{lendYearRate}/{period}/{investAmount}")
    public R getInterestCount(@PathVariable("returnMethod") Integer returnMethod,
                              @PathVariable("lendYearRate") BigDecimal lendYearRate,
                              @PathVariable("period") Integer period,
                              @PathVariable("investAmount") BigDecimal investAmount){
        BigDecimal interestCount  = userAccountService.getInterestCount(returnMethod,lendYearRate,period , investAmount);
        return R.success().data("interestCount",interestCount);
    }

    @ApiOperation("查询账户余额")
    @GetMapping("/auth/getAmount")
    public R getAmount(@RequestHeader(value = "token",required = false)
                        String token){
        BigDecimal amount = userAccountService.getAmount(token);
        return R.success().data("amount",amount);
    }
    //http://192.168.1.170/api/core/userAccount/notify
    @ApiOperation("充值完成的回调接口")
    @PostMapping("notify")
    public String chargeAmountNotify(HttpServletRequest request){ //线程间通信
        String result = userAccountService.chargeAmountNotify(request);
        return result;
    }

    @ApiOperation("充值")
    @PostMapping("/auth/chargeAmount")
    public R chargeAmount(@RequestParam("amount")BigDecimal amount,
                          @RequestHeader(value = "token",required = false)String token){
        String formStr = userAccountService.chargeAmount(amount,token);
        return R.success().data("formStr",formStr);
    }

}

